package com.moyu.user.service.permission.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.moyu.common.core.enums.ResultCode;
import com.moyu.common.security.exception.ServiceException;
import com.moyu.user.domain.permission.Permission;
import com.moyu.user.domain.permission.dto.PermissionQueryDTO;
import com.moyu.user.domain.permission.dto.PermissionSaveDTO;
import com.moyu.user.domain.permission.vo.PermissionVO;
import com.moyu.user.mapper.permission.PermissionMapper;
import com.moyu.user.service.permission.IPermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 权限服务层
 *
 * @Author 赵彦博
 * @Date 2024/12/27 15:31
 */
@Slf4j
@Service
public class PermissionServiceImpl implements IPermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public int add(PermissionSaveDTO permissionSaveDTO) {
        List<Permission> permissionList = new ArrayList<>();
        Permission permission = new Permission();
        try {
            permissionList = permissionMapper.selectList(new LambdaQueryWrapper<Permission>()
                    .eq(Permission::getName, permissionSaveDTO.getName()));
        } catch (Exception e) {
            log.error("执行sql-查询出错:" + e.getCause());
        }
        if (CollectionUtil.isNotEmpty(permissionList)) {
            //权限已经存在
            //自定义的异常   公共的异常类
            throw new ServiceException(ResultCode.AILED_PERMISSION_EXISTS);
        }
        permission.setName(permissionSaveDTO.getName());
        try {
            return permissionMapper.insert(permission);
        } catch (Exception e) {
            log.error("执行sql-新增权限出错:" + e.getCause());
        }
        return 0;
    }

    @Override
    public int addPermissionWithRole(Long id, List<PermissionVO> permissions) {
        // 权限名
        if (!permissions.isEmpty()) {
            return permissionMapper.addPermissionWithRole(id, permissions);
        }
        return 0;
    }

    @Override
    public int delete(Long id) {
        Permission permission = new Permission();
        try {
            permission = permissionMapper.selectById(id);
        } catch (Exception e) {
            log.error("执行sql-查询出错:" + e);
        }
        if (permission == null) {
            throw new ServiceException(ResultCode.FAILED_PERMISSION_NOT_EXISTS);
        }
        try {
            // 查询没有数据直接select 有数据都删
            List<Long> existId = permissionMapper.selectRoleAndPermission(id);
            if (!CollectionUtil.isNotEmpty(existId)) {
                return permissionMapper.deleteById(id);
            } else {
                return permissionMapper.deleteAllById(id);
            }
        } catch (Exception e) {
            log.error("执行sql-删除出错:" + e);
        }
        return 0;
    }

    @Override
    public List<PermissionVO> list(PermissionQueryDTO permissionQueryDTO) {
        PageHelper.startPage(permissionQueryDTO.getPageNum(), permissionQueryDTO.getPageSize());
        try {
            return permissionMapper.selectPermissionList(permissionQueryDTO);
        } catch (Exception e) {
            log.error("执行sql-查询出错:" + e.getCause());
        } finally {
            PageHelper.clearPage();
        }
        return new ArrayList<>();
    }

    @Override
    public List<PermissionVO> listByRoleId(PermissionQueryDTO permissionQueryDTO) {
        PageHelper.startPage(permissionQueryDTO.getPageNum(), permissionQueryDTO.getPageSize());
        try {
            return permissionMapper.selectPermissionListByRoleId(permissionQueryDTO);
        } catch (Exception e) {
            log.error("执行sql-查询出错:" + e.getCause());
        } finally {
            PageHelper.clearPage();
        }
        return new ArrayList<>();
    }

    @Override
    public List<PermissionVO> listByRoleIdAll(Long id) {
        try {
            return permissionMapper.selectPermissionListByRoleIdAll(id);
        } catch (Exception e) {
            log.info("执行sql-查询出错:" + e.getCause());
        }
        return new ArrayList<>();
    }
}
